package com.cnsugar.common.jdbctest.service;

import com.cnsugar.common.jdbctest.bean.SysUser;
import com.cnsugar.common.jdbctest.dao.SysUserDao;
import com.cnsugar.common.paging.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Sugar
 * @Version 2018/12/17 18:18
 */
@Service
public class SysUserService {
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 新增用户
     *
     * @param sysUser
     */
    public void save(SysUser sysUser) {
        Long id = sysUserDao.save(sysUser);
        if (id > 0) {
            System.out.println("save ok, id=" + id);
            sysUser.setId(id.intValue());
        } else {
            System.out.println("save fail");
        }
    }

    /**
     * 修改用户
     *
     * @param sysUser
     */
    public void update(SysUser sysUser) {
        long row = sysUserDao.update(sysUser);
        System.out.println("update " + row + " rows.");
    }

    /**
     * 根据ID删除用户
     *
     * @param id
     */
    public void delete(int id) {
        int row = sysUserDao.deleteById(id);
        System.out.println("delete " + row + " rows.");
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public SysUser findById(int id) {
        return sysUserDao.findById(id);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    public List<SysUser> findAll() {
        return sysUserDao.queryForList("1=1");
    }

    /**
     * 分页查询
     *
     * @param user     查询条件
     * @param pageSize 每页显示多少条记录
     * @param pageNo   第几页（从1开始）
     * @return
     */
    public Page findForPage(SysUser user, int pageSize, int pageNo) {
        Page page = new Page(pageSize, pageNo);
        page.setClazz(SysUser.class);
        return sysUserDao.find(user, page);
    }
}
